package com.saber.king.service.impl;

import com.github.pagehelper.PageHelper;
import com.saber.king.entity.Company;
import com.saber.king.entity.CompanyExample;
import com.saber.king.exception.BusinessException;
import com.saber.king.exception.code.BaseResponseCode;
import com.saber.king.mapper.CompanyMapper;
import com.saber.king.service.CompanyService;
import com.saber.king.utils.BeanUtils;
import com.saber.king.utils.PageUtil;
import com.saber.king.vo.req.CompanyAddReqVO;
import com.saber.king.vo.req.CompanyPageReqVO;
import com.saber.king.vo.req.CompanyUpdateReqVO;
import com.saber.king.vo.resp.PageRespVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @author Saber污妖王
 * TODO: 公司业务层实现类
 * @version 0.0.1
 * @editor Saber污妖王
 * @project customer-circulation
 * @date 2020/6/22
 * @package com.saber.king.service.impl
 */
@Service
public class CompanyServiceImpl implements CompanyService {
    @Resource
    private CompanyMapper companyMapper;

    @Override
    public List<Company> selectAll() {
        CompanyExample example = new CompanyExample();
        CompanyExample.Criteria criteria = example.createCriteria();
        criteria.andDeletedEqualTo(1);
        return companyMapper.selectByExample(example);
    }

    @Override
    public PageRespVO<Company> pageInfo(CompanyPageReqVO vo) {
        PageHelper.startPage(vo.getPageNum(), vo.getPageSize());
        CompanyExample example = new CompanyExample();
        CompanyExample.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(vo.getCompanyName())) {
            criteria.andCompanyNameLike("%" + vo.getCompanyName() + "%");
        }
        if (StringUtils.isNotBlank(vo.getLicenseKey())) {
            criteria.andLicenseKeyEqualTo(vo.getLicenseKey());
        }
        if (StringUtils.isNotBlank(vo.getRepresentative())) {
            criteria.andRepresentativeLike("%" + vo.getRepresentative() + "%");
        }
        if (StringUtils.isNotBlank(vo.getType())) {
            criteria.andTypeLike("%" + vo.getType() + "%");
        }
        List<Company> list = companyMapper.selectByExample(example);
        return PageUtil.getPageVO(list);
    }

    @Override
    public Company addCompany(CompanyAddReqVO vo, String userId) {
        Company company = new Company();
        BeanUtils.copyProperties(vo, company);
        company.setId(UUID.randomUUID().toString());
        company.setCreateId(userId);
        company.setCreateTime(new Date());
        int i = companyMapper.insertSelective(company);
        if (i != 1) {
            throw new BusinessException(BaseResponseCode.OPERATION_ERROR);
        }
        return company;
    }

    @Override
    public void updateCompany(CompanyUpdateReqVO vo, String userId) {
        Company company = new Company();
        BeanUtils.copyProperties(vo, company);
        company.setUpdateId(userId);
        company.setUpdateTime(new Date());
        int i = companyMapper.updateByPrimaryKeySelective(company);
        if (i != 1) {
            throw new BusinessException(BaseResponseCode.OPERATION_ERROR);
        }
    }

    @Override
    public void batchDeleteCompany(List<String> ids, String operationId) {
        Company company = new Company();
        company.setId(operationId);
        company.setUpdateTime(new Date());
        int i = companyMapper.deletedCompanies(company, ids);
        if (i == 0) {
            throw new BusinessException(BaseResponseCode.OPERATION_ERROR);
        }
    }

    @Override
    public Company getCompanyById(String id) {
        return companyMapper.selectByPrimaryKey(id);
    }
}
